package com.wj.tool.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.cookie.BasicClientCookie2;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.security.cert.X509Certificate;

@Slf4j
public class HtmlUtils {

    public static String getRawHTML ( String url ) throws ClientProtocolException, IOException, InterruptedException{
        //初始化
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.protocol.cookie-policy",
                CookiePolicy.BROWSER_COMPATIBILITY);
        //设置参数
        HttpParams params = httpclient.getParams();
        //连接时间
        HttpConnectionParams.setConnectionTimeout(params, 6000);
        HttpConnectionParams.setSoTimeout(params, 6000*20);
        //超时重新请求次数
        DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(5,true);
        HttpContext localContext = new BasicHttpContext();
        HttpRequest request2 = (HttpRequest) localContext.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        httpclient.setHttpRequestRetryHandler(dhr);
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie2 cookie = new BasicClientCookie2("Content-Type","text/html;charset=UTF-8");
        BasicClientCookie2 cookie1 = new BasicClientCookie2("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
        cookieStore.addCookie(cookie);
        cookieStore.addCookie(cookie1);
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        HttpGet request = new HttpGet();
        request.setURI(URI.create(url));
        HttpResponse response = null;
        String rawHTML = "";
        response = httpclient.execute(request,localContext);
        int StatusCode = response.getStatusLine().getStatusCode(); //获取响应状态码
        if(StatusCode == 200){                          //状态码200表示响应成功
            //获取实体内容
            rawHTML = EntityUtils.toString(response.getEntity());
            //输出实体内容
            EntityUtils.consume(response.getEntity());       //消耗实体
        }else {
            //关闭HttpEntity的流实体
            EntityUtils.consume(response.getEntity());        //消耗实体
        }
        httpclient.close();
        return rawHTML;
    }

    public static boolean connectUrl ( String url ) {
        // 跳过ssl证书校验
        disableSslVerification();
        //初始化
        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.protocol.cookie-policy",
                CookiePolicy.BROWSER_COMPATIBILITY);
        //设置参数
        HttpParams params = httpclient.getParams();
        //连接时间
        HttpConnectionParams.setConnectionTimeout(params, 6000);
        HttpConnectionParams.setSoTimeout(params, 6000*20);
        //超时重新请求次数
        DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(5,true);
        HttpContext localContext = new BasicHttpContext();
        HttpRequest request2 = (HttpRequest) localContext.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        httpclient.setHttpRequestRetryHandler(dhr);
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie2 cookie = new BasicClientCookie2("Content-Type","text/html;charset=UTF-8");
        BasicClientCookie2 cookie1 = new BasicClientCookie2("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
        cookieStore.addCookie(cookie);
        cookieStore.addCookie(cookie1);
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        HttpGet request = new HttpGet();
        request.setURI(URI.create(url));
        HttpResponse response = null;
        String rawHTML = "";
        try {
            response = httpclient.execute(request,localContext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int statusCode = response.getStatusLine().getStatusCode(); //获取响应状态码
        if (statusCode == 200){
            return true;
        }
        return false;
    }

    public static String readFileByUrl(String urlStr) {
        String res = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            InputStream inputStream = conn.getInputStream();
            res = readInputStream(inputStream);
        } catch (Exception e) {
            log.error("通过url地址获取文本内容失败",e);
        }
        return res;
    }


    /**
     * 从输入流中获取字符串
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return new String(bos.toByteArray(), "utf-8");
    }

    /**
     * 跳过Https的证书
     */
    public static void disableSslVerification() {
        try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    // TO
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    // TO
                }
            }
            };
            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = (hostname, session) -> true;
            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void getFirfoxGet(){
        String thisurl = "https://m.v.qq.com/x/m/play?cid=mzc00200p51jpn7&vid=j0043h9kf6x&ptag=v_qq_com%23v.play.adaptor%233";
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_68);//模拟火狐浏览器
        try {

//            HtmlPage page = webClient.getPage("http://www.baidu.com");
//            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.setJavaScriptTimeout(5000);
            webClient.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书
            webClient.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本
            webClient.getOptions().setCssEnabled(false);//禁用css支持
//            webClient.getOptions().set
            webClient.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常
            webClient.getOptions().setTimeout(100000);//设置连接超时时间
            webClient.getOptions().setDoNotTrackEnabled(false);
//            HtmlPage page = webClient.getPage("http://blog.csdn.net/su20145104009?viewmode=contents");
            HtmlPage page = webClient.getPage(thisurl);
//            String res=page.asText();
            Thread.sleep(3000);

            String html = page.asXml();
            System.out.println("-----------------------------------------------------");
            System.out.println("内容："+html);
            System.out.println("-----------------------------------------------------");
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally {
            webClient.close();
        }
    }


    public static void main(String[] args) throws IOException {
        System.out.println(readFileByUrl("https://www.biqugeg.cc/18_2554/"));
    }

}